package com.lightsh.things;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.contacts.Contact;
import org.jbox2d.dynamics.contacts.ContactPoint;

import com.lightsh.body.PolygonImpl;
import com.lightsh.gui.PhysicsScreen;
import com.lightsh.utility.Constants;
import com.lightsh.utility.Debugger;
import com.lightsh.utility.GeometryUtility;

public class Plane extends PolygonImpl{
	private static final String TAG = "Plane";
	
	public static final int VELOCITY = 8;
	
	private Vec2 mCurrentVelocity = new Vec2();
	private int mVelocityDirection = 0;
	
	private float mTargetAngel = 0;
	
	private boolean mEnd = false; 
	
	private final int mPlaneWidth;
	private final int mPlaneHeight;
	
	@Override
	public void update() {
		super.update();
		changeAngle();		
		
		if(mEnd){
			// FIXME 飞机失事,可以考虑destroy自己，并生成多个飞机碎片
			mParentScreen.vibrate(100);
			mWorld.destroyBody(mBody);
			Vec2 position = getPosition();
			position.x = mParentScreen.meterToPx(position.x);
			position.y = mParentScreen.meterToPx(position.y);
			float velocity = 10;
			int lastSecond = 8;
			
			PlaneChip planeChip = new PlaneChip(mParentScreen, position.x + 15, position.y, 1, 1, 0, 10, lastSecond);
			planeChip.setVelocityInfo(velocity, GeometryUtility.angleToRadian(0));
			planeChip.create();
			
			planeChip = new PlaneChip(mParentScreen, position.x, position.y + 15, 1, 1, 0, 10, lastSecond);
			planeChip.setVelocityInfo(velocity, GeometryUtility.angleToRadian(90));
			planeChip.create();
			
			planeChip = new PlaneChip(mParentScreen, position.x - 15, position.y, 1, 1, 0, 10, lastSecond);
			planeChip.setVelocityInfo(velocity, GeometryUtility.angleToRadian(180));
			planeChip.create();
			
			planeChip = new PlaneChip(mParentScreen, position.x, position.y - 15, 1, 1, 0, 10, lastSecond);
			planeChip.setVelocityInfo(velocity, GeometryUtility.angleToRadian(270));
			planeChip.create();
			
//			changeVelocity();
//			mParentScreen.vibrate(100);
//			mEnd = false;
		}
	}

	public Plane(PhysicsScreen screen, float x, float y, float density,
			float friction, float restitution, int planeWidth, int planeHeight) {
		super(screen, x, y, density, friction, restitution);
		
		mBodyDef.allowSleep = false;
		mBodyDef.fixedRotation = true;
		
		mPlaneWidth = planeWidth;
		mPlaneHeight = planeHeight;
		
		// 一个三角形
		final float halfWidth = mPlaneWidth / 2;
		final float disHeight = mPlaneHeight / 3;
		
		setDrawImageOffset(-halfWidth, -disHeight);
		
		add(new Vec2(-halfWidth, -disHeight), false);
		add(new Vec2(halfWidth, -disHeight), false);
		add(new Vec2(0, disHeight * 2), true);
	}
	
	public void updateVelocity(float xGravity){
		float absGravity = Math.abs(xGravity);
		if(absGravity <= 1){
			mVelocityDirection = 0;
		}else if(absGravity <= 2){
			mVelocityDirection = 1;
		}else if(absGravity <= 4){
			mVelocityDirection = 2;
		}else{
			mVelocityDirection = 3;
		}
		if(xGravity < 0){
			mVelocityDirection = -mVelocityDirection;
		}		
		changeVelocity();
	}
	
	public void onDirectionClick(boolean left){
		if(left){
			if(mVelocityDirection > -3){
				mVelocityDirection -= 1;
			}
		}else{
			if(mVelocityDirection < 3){
				mVelocityDirection += 1;
			}
		}		
		changeVelocity();
	}
	
	private void changeVelocity(){
		if(isEnd() || mBody == null){
			return;
		}
		switch(mVelocityDirection){
		case 0:
			mCurrentVelocity.x = 0;
			mCurrentVelocity.y = 6 * VELOCITY;	
			mTargetAngel = 0;
			break;
		case 1:
		case -1:
			mCurrentVelocity.x = VELOCITY;
			mCurrentVelocity.y = 4 * VELOCITY;	
			mTargetAngel = -20 * Constants.DEG_TO_RAD;
			break;
		case 2:
		case -2:
			mCurrentVelocity.x = 2 * VELOCITY;
			mCurrentVelocity.y = 2 * VELOCITY;	
			mTargetAngel = -40 * Constants.DEG_TO_RAD;
			break;
		case 3:
		case -3:
			mCurrentVelocity.x = 3 * VELOCITY;
			mCurrentVelocity.y = 1 * VELOCITY;	
			mTargetAngel = -60 * Constants.DEG_TO_RAD;
			break;
		}
		if(mVelocityDirection < 0){
			mCurrentVelocity.x = -mCurrentVelocity.x;
			mTargetAngel = -mTargetAngel;
		}
		
		Vec2 bodyVelocity = mBody.getLinearVelocity();
		if(!bodyVelocity.equals(mCurrentVelocity)){
//			Debugger.logT(TAG, "velocity -> " + mCurrentVelocity);
			mBody.setLinearVelocity(mCurrentVelocity);
		}
	}

	@Override
	public void onContactAdd(ContactPoint point) {
		mEnd = true;
	}
	
	
	@Override
	public void onContactPersist(ContactPoint point) {
		mEnd = true;
	}

	public boolean isEnd(){
		return mEnd;
	}
	
	private void changeAngle(){
		float angle = mBody.getAngle();
		if(!GeometryUtility.isEqual(angle, mTargetAngel, 0.01f)){
			float angleError = mTargetAngel - angle;
			float gain = 10f;
			mBody.setAngularVelocity(gain * angleError);
		}else{
			if(mBody.getAngularVelocity() != 0){
				mBody.setAngularVelocity(0);
			}
		}
	}

	@Override
	public void create() {
		super.create();
		
		// 初始化速度
		changeVelocity();
	}
	
}
